﻿
Imports System.IO
Imports SFDL.Core
Imports SFDL.Loader.My.Resources

Public Class frm_pluginmanager

    Dim _plugin_connector As New PluginConnector
    Dim _tools As New Tools
    Private Shared _log As NLog.Logger = NLog.LogManager.GetLogger("PluginManager")

    Private Sub frm_pluginmanager_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        _log.Info("Ermittle geladene Plugins....")

        _plugin_connector = GetPluginConnectorInstance

        GetPlugins()

        _log.Info("PluginManager", "Geladene PLugins ermittelt!")

    End Sub

    Private Sub GetPlugins()

        Dim _group_active As New ListViewGroup(StringMessages.PluginManagerGroupsActivatedPluginName, StringMessages.PluginManagerGroupsActivatedPluginName)
        Dim _group_disabled As New ListViewGroup(StringMessages.PluginManagerGroupsDisabledPluginName, StringMessages.PluginManagerGroupsDisabledPluginName)

        list_plugins.Items.Clear()
        list_plugins.Groups.Clear()

        list_plugins.Groups.Add(_group_active)
        list_plugins.Groups.Add(_group_disabled)

        For Each _plugin In Directory.GetFiles(GetPluginsPath, "*.dll")

            Dim _disable_item As String
            Dim _lv_item As New ListViewItem

            _disable_item = _plugin & ".disabled"

            _lv_item.UseItemStyleForSubItems = True
            _lv_item.Text = Path.GetFileNameWithoutExtension(_plugin)

            If IO.File.Exists(_disable_item) Then
                _lv_item.Group = _group_disabled
                _lv_item.StateImageIndex = 1
            Else
                _lv_item.Group = _group_active
                _lv_item.StateImageIndex = 0
            End If

            list_plugins.Items.Add(_lv_item)

        Next

    End Sub

    Private Sub cmd_reload_plugins_Click(sender As Object, e As EventArgs) Handles cmd_reload_plugins.Click
        GetPlugins()
    End Sub

    Private Sub cmd_ok_Click(sender As Object, e As EventArgs) Handles cmd_ok.Click
        Me.Close()
    End Sub

    Private Sub PluginDeakivierenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PluginDeakivierenToolStripMenuItem.Click

        For Each _item As ListViewItem In list_plugins.SelectedItems

            If _item.Group.Name = StringMessages.PluginManagerGroupsActivatedPluginName Then

                Dim _full_path As String = Path.Combine(GetPluginsPath, _item.Text & ".dll")

                If IO.File.Exists(_full_path) Then
                    IO.File.Create(_full_path & ".disabled")
                End If

            End If

            If _item.Group.Name = StringMessages.PluginManagerGroupsDisabledPluginName Then

                Dim _full_path As String = Path.Combine(GetPluginsPath, _item.Text & ".dll.disabled")

                If IO.File.Exists(_full_path) Then
                    IO.File.Delete(_full_path)
                End If

            End If

        Next

        GetPlugins()

        If MessageBox.Show(StringMessages.PluginManagerRestartPrompt, StringMessages.PluginManagerRestartTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Application.Restart()
        End If

    End Sub

    Private Sub cmd_add_plugin_Click(sender As Object, e As EventArgs) Handles cmd_add_plugin.Click

        If Not OFD_AddPlugin.ShowDialog = DialogResult.Cancel Then

            Dim _pluginpath As String = GetPluginsPath

            Try

                For Each _plugin In OFD_AddPlugin.FileNames

                    Dim _targetpath As String

                    _targetpath = Path.Combine(_pluginpath, Path.GetFileName(_plugin))

                    If IO.File.Exists(_targetpath) Then

                        If MessageBox.Show(StringMessages.PluginManagerOverrideExistingPluginPrompt, StringMessages.PluginManagerOverrideExistingPluginTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then

                            IO.File.Create(_targetpath & ".delete")

                        End If

                    End If

                    IO.File.Copy(_plugin, _targetpath & ".new")

                Next

                GetPlugins()

                MessageBox.Show(StringMessages.PluginManagerInstallNewPluginSuccessPrompt, "PluginManager", MessageBoxButtons.OK, MessageBoxIcon.Information)

                If MessageBox.Show(StringMessages.PluginManagerRestartPrompt, StringMessages.PluginManagerRestartTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                    Application.Restart()
                Else
                    MessageBox.Show(StringMessages.PluginManagerRestartDeniedPrompt, "PluginManager", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If

            Catch ex As Exception
                _log.Error(ex, ex.Message)
                MessageBox.Show(String.Format(StringMessages.PluginManagerInstallNewPluginErrorPrompt, ex.Message), StringMessages.PluginManagerInstallNewPluginErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

        End If

    End Sub

End Class